package com.sduonline.isdapp.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sduonline.isdapp.jdbc.ArticleDao;
import com.sduonline.isdapp.model.temp.ArticleBrief;

@WebServlet("/SearchArticlesServlet")
public class SearchArticlesServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public SearchArticlesServlet() {
    	super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {

		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		// 前端main.js获取失败，why?
		response.setHeader("Access-Control-Allow-Origin", "*");

		String result = "-1";// 初始化失败
		Boolean allcontent=false;//判断是否获得全部内容  1代表全部，0代表部分
		String number = request.getParameter("number");
		String keyword = request.getParameter("keyword");
		String source = request.getParameter("source");
		String type = request.getParameter("type");
		String picture = request.getParameter("picture");
		String date = request.getParameter("date");
		String isAllContent=request.getParameter("content");
		
		/*System.out.println("SearchArticlesServlet-number:" + number);
		System.out.println("SearchArticlesServlet-keyword:" + keyword);
		System.out.println("SearchArticlesServlet-source:" + source);
		System.out.println("SearchArticlesServlet-type:" + type);
		System.out.println("SearchArticlesServlet-picture:" + picture);
		System.out.println("SearchArticlesServlet-date:" + date);*/

		PrintWriter pw = new PrintWriter(response.getOutputStream());

		try {

			// keyword为中文，需要给decode一下
			if (keyword != null && !"".equals(keyword)) {
				keyword = URLDecoder.decode(keyword, "UTF-8");
				//System.out.println("keyword after decode:" + keyword);
			}

			// 全部设置成默认值
			if ("".equals(number) || number == null) {
				number = "0";
			}
			if ("".equals(source) || source == null) {
				source = "0";
			}
			if ("".equals(type) || type == null) {
				type = "0";
			}
			if ("".equals(picture) || picture == null) {
				picture = "0";
			}
			if ("".equals(date) || date == null) {
				date = "0";
			}
			if ("".equals(keyword) || null == keyword) {
				keyword = "";
			}
			if(isAllContent!=null&&isAllContent.endsWith("1"))
				allcontent=true;
			long inumber = Long.parseLong(number);
			
			String[] stemp=source.split("/");
			int []sourceId = new int[stemp.length];
			for(int i=0;i<stemp.length;i++){
				sourceId[i]=Integer.parseInt(stemp[i]);
			}
			int typeId = Integer.parseInt(type);
			int pictureNumber = Integer.parseInt(picture);
			int dateId = Integer.parseInt(date);

			ArticleDao adao = new ArticleDao();
			List<ArticleBrief> list = adao.searchArticles(inumber, keyword, sourceId, typeId, pictureNumber, dateId);

			JSONObject js = new JSONObject();
			if (list != null) {
				// 将content只保留前80个字
				for (ArticleBrief ab : list) {
					if (ab != null&&(!allcontent)) {
						//System.out.print(ab.getId() + " ,");
						String content = ab.getContent();
						if (content != null) {
							Document doc = Jsoup.parse(content);
							String text = doc.text().length() > 80 ? doc.text().substring(0, 80) : doc.text();
							ab.setContent(text);
						}
					}
				}
				System.out.println();

				js.put("list", JSONArray.toJSON(list));
			} else {
				js.put("list", null);
			}
			result = js.toString();
		} catch (NumberFormatException e) {
			result = "-2";// 服务器出错
			e.printStackTrace();
		}

		//System.out.println("in SearchArticlesServlet,result=" + result);
		pw.write(result);
		pw.flush();
		pw.close();

	}

}
